//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package imagetoolscommon;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import javax.swing.JComponent;

public class ImagePanel extends JComponent {
    private Image image;
    private int width;
    private int height;
    private Rectangle imageRect;

    public ImagePanel() {
    }

    public Rectangle getImageRectangle() {
        return this.imageRect;
    }

    public Rectangle getSquareImageRectangle() {
        if(this.imageRect == null) {
            return null;
        } else {
            Rectangle rect = null;
            int size;
            if(this.imageRect.width > this.imageRect.height) {
                size = this.imageRect.width;
                rect = new Rectangle(this.imageRect.x, this.imageRect.y - (this.imageRect.width - this.imageRect.height) / 2, size, size);
            } else {
                size = this.imageRect.height;
                rect = new Rectangle(this.imageRect.x - (this.imageRect.height - this.imageRect.width) / 2, this.imageRect.y, size, size);
            }

            return rect;
        }
    }

    public ImagePanel(Image image) {
        this.setImage(image);
    }

    public Image getImage() {
        return this.image;
    }

    public void setImage(Image image) {
        this.image = image;
        if(image != null) {
            this.width = image.getWidth(this);
            if(this.width <= 0) {
                MediaTracker tracker = new MediaTracker(this);
                tracker.addImage(image, 0);

                try {
                    tracker.waitForAll();
                } catch (InterruptedException var4) {
                    ;
                }

                this.width = image.getWidth(this);
            }

            this.height = image.getHeight(this);
            this.setPreferredSize(new Dimension(this.width, this.height));
        }
    }

    public void paintComponent(Graphics g) {
        if(this.image != null) {
            float fm = (float)this.width / (float)this.height;
            float text = (float)this.getWidth() / (float)this.getHeight();
            int w;
            int offX;
            if(fm > text) {
                w = (int)((float)this.getWidth() / fm);
                offX = (this.getHeight() - w) / 2;
                this.imageRect = new Rectangle(0, offX, this.getWidth(), w);
                g.drawImage(this.image, 0, offX, this.getWidth(), w, this);
            } else {
                w = (int)((float)this.getHeight() * fm);
                offX = (this.getWidth() - w) / 2;
                this.imageRect = new Rectangle(offX, 0, w, this.getHeight());
                g.drawImage(this.image, offX, 0, w, this.getHeight(), this);
            }
        } else {
            g.setColor(Color.black);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            FontMetrics fm1 = g.getFontMetrics();
            g.setColor(Color.white);
            String text1 = "No Image Loaded";
            g.drawString(text1, (this.getWidth() - fm1.stringWidth(text1)) / 2, this.getHeight() / 2);
        }

    }
}
